package com.akk.module.app.service;


import com.akk.module.app.entity.App;
import com.akk.module.app.entity.AppExample;
import com.akk.module.app.entity.AppUserGradeAccessExample;
import com.akk.module.app.mapper.AppMapper;
import com.akk.module.app.mapper.AppUserGradeAccessMapper;
import com.akk.module.app.mapper.AppUserGradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AppService {
    @Autowired
    AppMapper appMapper;
    @Autowired
    AppExample appExample;
    @Autowired
    AppUserGradeAccessMapper appUserGradeAccessMapper;
    @Autowired
    AppUserGradeAccessExample appUserGradeAccessExample;
    @Autowired
    AppUserGradeMapper appUserGradeMapper;


    @Transactional
    public void save(App app) {
/*        gradeExample.clear();
        appGradeExample.clear();
        List<Grade> grades = gradeMapper.selectByExample(gradeExample);

        for(Grade grade:grades){
            AppGrade appGrade = new AppGrade();
            appGrade.setAppId(app.getAppId());
            appGrade.setGradeName(grade.getGradeDefaultName());
            appGrade.setMoney(grade.getGradeMinPrice());
            appGrade.setRebate(grade.getGradeMinRebate());
            appGrade.setStatus(AppGradeEnums.StatusEnum.NORMAL.getValue());
            appGrade.setGradeId(grade.getGradeId());
            appGradeMapper.insert(appGrade);
        }*/
        appMapper.insert(app);
    }

    public void update(App app){
        App record = appMapper.selectByPrimaryKey(app.getAppId());
        record.setAdvice(app.getAdvice());
        record.setVersion(app.getVersion());
        record.setHotLine(app.getHotLine());
        record.setAppStatus(app.getAppStatus());
/*        record.setMaxAgentLevelType(app.getMaxAgentLevelType());
        record.setOpenAgentLevel(app.getOpenAgentLevel());
        record.setGradeUpAccess(app.getGradeUpAccess());*/
        record.setAppCashAccessId(app.getAppCashAccessId());
        record.setAppD0Cash(app.getAppD0Cash());
        record.setAppT1Cash(app.getAppT1Cash());
        record.setUpdateDate(new Date());
        appMapper.updateByPrimaryKey(record);
    }

    public List<App> findByParam(Map param) {
        appExample.clear();
        AppExample.Criteria criteria = appExample.createCriteria();
        if(param.containsKey("appName")){
            criteria.andAppNameLike(param.get("appName").toString());
        }
        if(param.containsKey("oemName")){
            criteria.andOemNameLike(param.get("oemName").toString());
        }
        return appMapper.selectByExample(appExample);
    }

    public App findByAppId(String appId) {
        return appMapper.selectByPrimaryKey(appId);
    }

    public List<App> findByStatus(Integer value) {
        appExample.clear();
        appExample.createCriteria().andAppStatusEqualTo(value);
        return appMapper.selectByExample(appExample);
    }
}
